package com.mr.apigateway.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;

import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.POST_TYPE;
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.SEND_RESPONSE_FILTER_ORDER;

/**
 * Class: AddResponseHeaderFilter
 * User (作者):MRui
 * TODO (描述)：
 * Date 2018-11-14 23:44
 */
@Component
public class AddResponseHeaderFilter extends ZuulFilter {

    /**返回过滤器的类型。有pre、route、post、error等几种取值，分别对应上文的几种过滤器。详细可以参考com.netflix.zuul.ZuulFilter.filterType() 中的注释。*/
    @Override
    public String filterType() {
        //前置过滤器PRE_TYPE（请求前） 可用来限流 鉴权 参数校验，请求转发等
        //POST_TYPE（请求后） 后置过滤器 可用记录统计，日志等
        return POST_TYPE;
    }
    /**执行顺序*/
    @Override
    public int filterOrder() {
        //在SEND_RESPONSE_FILTER_ORDER之前
        return SEND_RESPONSE_FILTER_ORDER -1;
    }
    /**是否执行*/
    @Override
    public boolean shouldFilter() {
        return true;
    }
    /**filter内容*/
    @Override
    public Object run() throws ZuulException {

        RequestContext requestContext = RequestContext.getCurrentContext();
        HttpServletResponse response = requestContext.getResponse();
        response.setHeader("Context","1111111");
        return null;
    }
}
